<template>
	<view class="page">
		<!-- 搜索 -->
		<view class="search-head">
			<view class="back" @click="onBack">
				<text></text>
			</view>
			<view class="search">
				<text class="iconfont icon-fadajing"></text>
				<input type="text" v-model="keyword" placeholder="搜索商品" @confirm="searchs" />
			</view>
			<view class="cut" @click="isList = !isList">
				<text class="iconfont" :class="isList?'icon-shitu01':'icon-shitu02'"></text>
			</view>
		</view>
		<!-- 筛选 -->
		<view class="screen-info">
			<view class="screen-list">
				<view class="list" :class="{'action':order==='sort'}" @click="onSynthesize('sort')">
					<text>综合</text>
				</view>
				<view class="list" :class="{'action':order==='stock_sales'}" @click="onSynthesize('stock_sales')">
					<text>销量</text>
					<text></text>
				</view>
				<view class="list" :class="{'action':order==='price_selling'}" @click="onSynthesize('price_selling',1)">
					<text>价格</text>
					<view class="icon_j">
						<text class="iconfont icon-sanjiao up" :style="sort=='desc'?'color: #555;':''"></text>
						<text class="iconfont icon-sanjiao down" :style="sort=='asc'?'color: #555;':''"></text>
					</view>
				</view>
				
			</view>
			
		</view>
		<!-- 商品列表 -->
		<view class="goods-data">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption" :top="0">
				<view class="goods-list">
					<view :class="isList?'list-view':'list-li'" v-for="(item,index) in goodsList" @click="onGoodsList(item.code)" :key="index" v-if="goodsList.length!=0">
						<view class="thumb">
							<image :src="item.cover" mode="heightFix"></image>
						</view>
						<view class="item">
							<view class="title">
								<text class="one-omit">{{item.name}}</text>
							</view>
							<view class="price">
								<view class="retail-price">
									<text class="min">￥</text>
									<text class="max">{{item.price_selling}}</text>
								</view>
								<view class="vip-price">
									<text class="min">￥</text>
									<text class="max">{{item.price_market}}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="empty-box"  v-if="goodsList.length==0" style="padding-top: 100rpx;">
						<image src="../../static/ic_nodata.png" mode=""></image>
						<view class="">暂无数据</view>
					</view>
				</view>
			</mescroll-body>
		</view>
		
	</view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				type:0,
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
				},
				// 列表视图切换
				isList: true,
				// 筛选弹窗
				isScreen: false,
				// 筛选切换
				screenShow: 0,
				// 抽屉
				isDrawer: false,
				keyword: '',
				goodsList:[
				 
				],
				page:1,
				order:'sort',
				sort:'asc',
				entrance_id:''
			}
		},
		onLoad(params) {
			this.keyword = decodeURIComponent(params.keyword||'');
			this.entrance_id = params.id
		},
		onShow() {
			let that = this
			that.page=1
			that.goodsList = []
			that.getList()
		},
		onReachBottom() {
			let that = this
			that.page++
			that.getList()
		},
		// #ifdef MP
		onShareAppMessage() {
			return {
				title:'分享海报',
				path:'/pages/home/home?ref='+uni.getStorageSync('inviter_code'),
			};
		},
		// #endif
		methods: {
			/*下拉刷新的回调, 有三种处理方式:*/
			downCallback(){
				this.mescroll.endSuccess();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				setTimeout(() =>{
					this.mescroll.endByPage(10, 20);
				},2000)
			},
			searchs(){
				let that = this
				that.page=1
				that.goodsList =[]
				that.getList()
				
			},
			getList(){
				let that = this
				that.tui.request("api.data/goods", "POST", {page:that.page,keyword:that.keyword,order:that.order,sort:that.sort,entrance_id:that.entrance_id}, false, false, true).then((res) => {
					if (res.code == 1) {
						that.goodsList = that.goodsList.concat(res.data.list)
					}
				}).catch((res) => {})
			},
			/**
			 * 返回点击
			 */
			onBack(){
				uni.navigateBack();
			},
			/**
			 * 综合点击
			 * @param {Number} type
			 */
			onSynthesize(type,d){
				let that = this
				if(d==1&&that.sort=='asc'){
					that.sort = 'desc'
				}else if(d==1&&that.sort=='desc'){
					that.sort = 'asc'
				}
				that.order = type;
				that.isScreen = !that.isScreen;
				that.page=1
				that.goodsList = []
				that.getList()
				console.log(that.sort)
			},
			/**
			 * 商品列表点击
			 */
			onGoodsList(e){
				uni.navigateTo({
					url: '/pages/GoodsDetails/GoodsDetails?id='+e,
					animationType: 'zoom-fade-out',
					animationDuration: 200
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import  'SearchGoodsList.scss';
</style>
